﻿using System;
using System.ComponentModel;
using System.Linq.Expressions;

namespace SmartCardClient.ViewModels
{
	public abstract class ViewModel : INotifyPropertyChanged
	{
		#region events

		public event PropertyChangedEventHandler PropertyChanged;

		#endregion

		#region publics

		public string Changed<TProp>(Expression<Func<TProp>> expr)
		{
			if (expr == null)
			{ throw new ArgumentNullException("expre"); }

			var body = expr.Body as MemberExpression;
			if (body == null)
			{ throw new ArgumentException("expression must be a property or field name"); }

			string name = body.Member.Name;
			return Changed(name);
		}

		public string Changed(string name)
		{
			if (name == null)
			{ throw new ArgumentNullException("name"); }

			PropertyChangedEventArgs eventArgs = new PropertyChangedEventArgs(name);
			SendPropertyChangedEvent(eventArgs);

			return name;
		}

		#endregion

		protected void SendPropertyChangedEvent(PropertyChangedEventArgs eventArgs)
		{
			if (eventArgs == null)
			{ throw new ArgumentNullException("eventArgs"); }

			if (PropertyChanged != null)
			{ PropertyChanged(this, eventArgs); }
		}
	}
}
